package org.game.module.event.listener;

import java.util.List;
import java.util.Map.Entry;

import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.game.db.bean.Hero;
import org.game.db.bean.Player;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventStack;
import org.game.plugin.event.GameEventType;

import com.sojoys.artifact.tools.ToolMap;

/**
 * @author : DengYing
 * @CreateDate : 2018年1月11日 下午2:59:44
 * @Description ：天使数据变更监听
 */
@GameEventType(EventType.ANGEL_CHANGE)
public class AngelChangeEventListenner implements GameEventListener {

	@Override
	public void onEvent(GameEvent event) {
		GameEventStack eventStack = event.peek();
		switch (eventStack.getConditionType()) {
			case EventConditionType.ANGEL_STAR_LVL:{
				if (checkTriggerType(event.getIn(), EventConditionType.ANGEL_STAR_LVL)) {
					int oldLv = ToolMap.getInt(EventConditionKey.OLD_LEVEL, event.getIn());
					int nowLv = ToolMap.getInt(EventConditionKey.NOW_LEVEL, event.getIn());
					int progress = eventStack.getMaxProgress();
					if (verify_eq(EventConditionKey.HERO_CID, event.getIn(), eventStack.getParams()) && oldLv < progress) {
						eventStack.setProgress(nowLv);
					}
				}
				break;
			}
			case EventConditionType.ANGEL_STAR_LVL_COUNT: {
				//天使升星事件触发拥有某星数天使数量事件
				if (checkTriggerType(event.getIn(), EventConditionType.ANGEL_STAR_LVL)) {
					int oldLv = ToolMap.getInt(EventConditionKey.OLD_LEVEL, event.getIn());
					int nowLv = ToolMap.getInt(EventConditionKey.NOW_LEVEL, event.getIn());
					int limitLvl = ToolMap.getInt(EventConditionKey.LIMIT_LEVEL, eventStack.getParams());
					if (oldLv < limitLvl && nowLv >= limitLvl) {
						eventStack.changeProgress(1);
					}
				}
				break;
			}
		}
	}

	@Override
	public void initEventCondition(Player player, GameEventStack eventStack) {
		switch (eventStack.getConditionType()) {
		case EventConditionType.ADVANCED_LVL: {
			List<Hero> heros = player.getHerosFkPlayerId();
			int maxLvl = 0;
			int heroCid = ToolMap.getInt(EventConditionKey.HERO_CID, eventStack.getParams());
			for (Hero hero : heros) {
				if (heroCid != 0 && heroCid != hero.getCid()) {
					continue;
				}
				maxLvl = Math.max(maxLvl, hero.getAngelLvl());
			}
			eventStack.setProgress(maxLvl);
		}
			break;
		case EventConditionType.ADVANCED_LVL_COUNT: {
			List<Hero> heros = player.getHerosFkPlayerId();
			int limitLvl = ToolMap.getInt(EventConditionKey.LIMIT_LEVEL, eventStack.getParams());
			int heroCid = ToolMap.getInt(EventConditionKey.HERO_CID, eventStack.getParams());
			int count = 0;
			for (Hero hero : heros) {
				if (heroCid != 0 && heroCid != hero.getCid()) {
					continue;
				}
				if (hero.getAdvancedLvl() < limitLvl) {
					continue;
				}
				count++;
			}
			eventStack.setProgress(count);
		}
		default:
			break;
		}
	}

}
